package com.teacher.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teacher.common.CommonResult;
import com.teacher.common.JwtTokenUtil;
import com.teacher.mapper.ClassTeacherMapper;
import com.teacher.pojo.dto.classteacher.ClassTeacherAddDto;
import com.teacher.pojo.dto.classteacher.ClassTeacherGetDto;
import com.teacher.pojo.dto.classteacher.ClassTeacherUpdateDto;
import com.teacher.pojo.entity.ClassTeacher;
import com.teacher.pojo.vo.classteacher.ClassTeacherGetVo;
import com.teacher.pojo.vo.classteacher.ClassTeacherListVo;
import com.teacher.service.IClassTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-07-18
 */
@Service
public class ClassTeacherServiceImpl extends ServiceImpl<ClassTeacherMapper, ClassTeacher> implements IClassTeacherService {

    @Autowired
    private ClassTeacherMapper classTeacherMapper;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 添加班主任
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<String> add(ClassTeacherAddDto dto) {
        // 检验是否重复
        if (this.getOne(new QueryWrapper<ClassTeacher>().lambda()
                .eq(ClassTeacher::getClassId, dto.getClassId())
                .eq(ClassTeacher::getTeacherKey, dto.getTeacherKey()).last("limit 1")) != null) {
            return CommonResult.failed("请勿重复添加");
        }
        ClassTeacher classTeacher = new ClassTeacher();
        BeanUtils.copyProperties(dto, classTeacher);
        if (this.save(classTeacher)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 查询班主任班级关联信息
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<IPage<ClassTeacherGetVo>> get(ClassTeacherGetDto dto) {
        Page<ClassTeacherGetVo> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        IPage<ClassTeacherGetVo> iPage = classTeacherMapper.getPage(page, dto);
        return CommonResult.success(iPage);
    }

    /**
     * 更新班主任班级信息
     *
     * @param dto
     * @return
     */
    @Override
    public CommonResult<String> put(ClassTeacherUpdateDto dto) {
        if (this.count(new QueryWrapper<ClassTeacher>().lambda()
                .eq(ClassTeacher::getClassId, dto.getClassId())
                .eq(ClassTeacher::getTeacherKey, dto.getTeacherKey())) > 1) {
            return CommonResult.failed("班主任信息重复");
        }
        System.out.println(JSONUtil.toJsonStr(dto) + "AAAA");
        ClassTeacher classTeacher = new ClassTeacher();
        BeanUtils.copyProperties(dto, classTeacher);
        System.out.println(JSONUtil.toJsonStr(classTeacher));
        if (this.updateById(classTeacher)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 班主任查询班级
     *
     * @return
     */
    @Override
    public CommonResult<List<ClassTeacherListVo>> getClassTeacherList() {
        List<ClassTeacherListVo> teacherListVoList = baseMapper.getClassTeacherList(jwtTokenUtil.getUsernameByRequest(httpServletRequest));
        return CommonResult.success(teacherListVoList);
    }
}
